package cd.safe.school.activity;

import java.util.List;

import org.androidpn.client.ServiceManager;

import android.app.ActivityGroup;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cd.safe.school.R;
import cd.safe.school.adapter.CompostionAdapter;
import cd.safe.school.adapter.EnglishEditAdapter;
import cd.safe.school.adapter.ErrorLibraryAdapter;
import cd.safe.school.adapter.SynparseAdapter;
import cd.safe.school.util.ListItemData;

public class HomeActivity extends ActivityGroup implements OnClickListener {
	public static int rutn;
	public static int uag;
	private Button title_butn;
	private TextView title_text;
	private LinearLayout activity_content;
	private LinearLayout botton_01;
	private LinearLayout botton_02;
	private LinearLayout botton_03;
	private LinearLayout botton_04;
	private LinearLayout botton_05;
	private LinearLayout botton_06;
	private Button title_fanhui;
	private String tag = "发送";
	private ProgressDialog pd;
	private String uploadurl = "http://192.168.0.101:8080/app/file/upload";
	public Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			Intent intent = null;
			switch (msg.what) {
			case 6:
				intent = new Intent(HomeActivity.this, Schoolmien_detail.class);
				intent.putExtras(msg.getData());
				HomeActivity.this.setActivity(intent);
				break;

			// case 2:
			// intent = new Intent(HomeActivity.this, CompositionDetail.class);
			// intent.putExtras(msg.getData());
			//
			// HomeActivity.this.setActivity(intent);
			// break;
			case 3:

				intent = new Intent(HomeActivity.this, EngilshActivity.class);
				intent.putExtras(msg.getData());
				HomeActivity.this.setActivity(intent);
				break;
			case 4:

				title_text.setText("听力编辑");
				title_butn.setBackgroundResource(R.drawable.upload);
				intent = new Intent(HomeActivity.this,
						EnglishEditDetailActivity.class);
				intent.putExtras(msg.getData());
				HomeActivity.this.setActivity(intent);
				break;
			case 5:
				intent = new Intent(HomeActivity.this, Schoolmien_detail.class);
				HomeActivity.this.setActivity(intent);
				break;
			case 1001:
				Toast.makeText(HomeActivity.this, "提取数据出错", Toast.LENGTH_SHORT)
						.show();
				break;
			}

		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		SchoolMienActivity.handler = handler;
		EnglishEditActivity.handler = handler;
		EnglishEditAdapter.handler = handler;
		SuperCompositionActivity.handler2 = handler;
		CompostionAdapter.handler = handler;
		SynParseActivity.handler2 = handler;
		SynparseAdapter.handler = handler;
		ErrorLibraryActivity.handler2 = handler;
		ErrorLibraryAdapter.handler = handler;

		// 获取ID后开启服务
		Intent loginintent = getIntent();
		String stringValue = loginintent.getStringExtra("userID");
		if (stringValue != null) {
			// 开起androidpn
			ServiceManager serviceManager = new ServiceManager(
					HomeActivity.this);
			serviceManager.setNotificationIcon(R.drawable.notification);
			serviceManager.setUserName("clientuser" + stringValue);
			serviceManager.startService();
		}
		title_fanhui = (Button) findViewById(R.id.title_fanhui);
		title_butn = (Button) findViewById(R.id.title_butn);
		title_text = (TextView) findViewById(R.id.title_text);
		activity_content = (LinearLayout) findViewById(R.id.activity_content);
		botton_01 = (LinearLayout) findViewById(R.id.botton_01);
		botton_02 = (LinearLayout) findViewById(R.id.botton_02);
		botton_03 = (LinearLayout) findViewById(R.id.botton_03);
		botton_04 = (LinearLayout) findViewById(R.id.botton_04);
		botton_05 = (LinearLayout) findViewById(R.id.botton_05);
		botton_06 = (LinearLayout) findViewById(R.id.botton_06);
		title_butn.setText("发送");
		title_butn.setOnClickListener(this);
		botton_01.setOnClickListener(this);
		botton_02.setOnClickListener(this);
		botton_03.setOnClickListener(this);
		botton_04.setOnClickListener(this);
		botton_05.setOnClickListener(this);
		botton_06.setOnClickListener(this);

		title_fanhui.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				HomeActivity.this.onKeyDown(KeyEvent.KEYCODE_BACK, null);

			}
		});
		title_butn.setVisibility(View.INVISIBLE);
		Intent intent = new Intent(this, SchoolMienActivity.class);

		Window suActivity = getLocalActivityManager().startActivity(
				"suActivity", intent);
		// 将view添加到相对应的容器里面
		//
		activity_content.addView(suActivity.getDecorView(),
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		// MainActivity.ret();
	}

	@Override
	public void onClick(View v) {
		title_butn.setVisibility(View.VISIBLE);
		if (v.getId() != R.id.title_butn) {
			// setcolor(v);
			activity_content.removeAllViews();
			title_text.setText("平安校园");
			Intent intent = null;
			tag = "上传";
			title_butn.setBackgroundResource(R.drawable.title_butn);
			switch (v.getId()) {
			case R.id.botton_01:
				tag = "发送";

				// MainActivity.ret();
				intent = new Intent(HomeActivity.this, MainActivity.class);

				break;
			case R.id.botton_02:

				intent = new Intent(HomeActivity.this,
						EnglishEditActivity.class);
				title_text.setText("听写英语");
				break;
			case R.id.botton_03:
				intent = new Intent(HomeActivity.this,
						SuperCompositionActivity.class);

				break;
			case R.id.botton_04:
				intent = new Intent(HomeActivity.this, SynParseActivity.class);

				break;

			case R.id.botton_05:
				intent = new Intent(HomeActivity.this,
						ErrorLibraryActivity.class);

				break;
			case R.id.botton_06:
				title_butn.setVisibility(View.INVISIBLE);
				intent = new Intent(HomeActivity.this, SchoolMienActivity.class);
				break;

			}
			title_butn.setText(tag);
			Window suActivity = getLocalActivityManager().startActivity(
					"suActivity", intent);
			// 将view添加到相对应的容器里面
			activity_content.addView(suActivity.getDecorView(),
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		} else {
			if ("发送".equals(tag)) {
				List<ListItemData> lis = MainActivity.getdata();
				if (lis.size() > 0) {
					String[] sendids = new String[lis.size()];
					for (int i = 0; i < lis.size(); i++) {
						sendids[i] = lis.get(i).getDeptid();
					}

					// if (MainActivity.tog == 1) {
					// // 选择学校发送
					//
					// } else if (MainActivity.tog == 2) {
					// // 选择班级发送
					// } else if (MainActivity.tog == 3) {
					// // 选择学生发送
					//
					// }

					Intent intent = new Intent(HomeActivity.this,
							SchoolSMS_detail.class);
					intent.putExtra("sendids", sendids);
					startActivity(intent);
				} else {
					Toast.makeText(HomeActivity.this, "请选择联系人",
							Toast.LENGTH_SHORT).show();
				}

			} else if (rutn == 4 || rutn == 8 || rutn == 10 || rutn == 9) {

				pd = ProgressDialog.show(HomeActivity.this, "", "文件上传中...");

				EnglishEditDetailActivity.upload();

				pd.dismiss();

			} else {
				Message msg = new Message();
				msg.what = 4;
				Bundle b = msg.getData();
				if (uag == 2) {
					b.putString("tag", "composition");

				} else if (uag == 3) {
					b.putString("tag", "synparse");

				} else if (uag == 4) {
					b.putString("tag", "error");

				} else if (uag == 1) {
					b.putString("tag", "english");

				}

				handler.sendMessage(msg);

			}

		}

	}

	// public void setcolor(View l) {
	// switch (l.getId()) {
	// case R.id.botton_01:
	// botton_01.setBackgroundColor(Color.RED);
	// botton_02.setBackgroundColor(Color.TRANSPARENT);
	// botton_03.setBackgroundColor(Color.TRANSPARENT);
	// botton_04.setBackgroundColor(Color.TRANSPARENT);
	// botton_05.setBackgroundColor(Color.TRANSPARENT);
	// botton_06.setBackgroundColor(Color.TRANSPARENT);
	//
	// break;
	// case R.id.botton_02:
	// botton_01.setBackgroundColor(Color.TRANSPARENT);
	// botton_02.setBackgroundColor(Color.RED);
	// botton_03.setBackgroundColor(Color.TRANSPARENT);
	// botton_04.setBackgroundColor(Color.TRANSPARENT);
	// botton_05.setBackgroundColor(Color.TRANSPARENT);
	// botton_06.setBackgroundColor(Color.TRANSPARENT);
	//
	// break;
	// case R.id.botton_03:
	// botton_01.setBackgroundColor(Color.TRANSPARENT);
	// botton_02.setBackgroundColor(Color.TRANSPARENT);
	// botton_03.setBackgroundColor(Color.RED);
	// botton_04.setBackgroundColor(Color.TRANSPARENT);
	// botton_05.setBackgroundColor(Color.TRANSPARENT);
	// botton_06.setBackgroundColor(Color.TRANSPARENT);
	//
	// break;
	// case R.id.botton_04:
	// botton_01.setBackgroundColor(Color.TRANSPARENT);
	// botton_02.setBackgroundColor(Color.TRANSPARENT);
	// botton_03.setBackgroundColor(Color.TRANSPARENT);
	// botton_04.setBackgroundColor(Color.RED);
	// botton_05.setBackgroundColor(Color.TRANSPARENT);
	// botton_06.setBackgroundColor(Color.TRANSPARENT);
	//
	// break;
	// case R.id.botton_05:
	// botton_01.setBackgroundColor(Color.TRANSPARENT);
	// botton_02.setBackgroundColor(Color.TRANSPARENT);
	// botton_03.setBackgroundColor(Color.TRANSPARENT);
	// botton_04.setBackgroundColor(Color.TRANSPARENT);
	// botton_05.setBackgroundColor(Color.RED);
	// botton_06.setBackgroundColor(Color.TRANSPARENT);
	// break;
	// case R.id.botton_06:
	// botton_01.setBackgroundColor(Color.TRANSPARENT);
	// botton_02.setBackgroundColor(Color.TRANSPARENT);
	// botton_03.setBackgroundColor(Color.TRANSPARENT);
	// botton_04.setBackgroundColor(Color.TRANSPARENT);
	// botton_05.setBackgroundColor(Color.TRANSPARENT);
	// botton_06.setBackgroundColor(Color.RED);
	//
	// break;
	//
	// }
	// }
	//
	public void setActivity(Intent intent) {
		activity_content.removeAllViews();
		// Intent intent = new Intent(HomeActivity.this, activity);
		Window suActivity = getLocalActivityManager().startActivity(
				"suActivity", intent);
		// 将view添加到相对应的容器里面
		activity_content.addView(suActivity.getDecorView(),
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// Toast.makeText(HomeActivity.this, rutn + "", Toast.LENGTH_SHORT)
			// .show();
			Intent intent = null;

			switch (rutn) {
			case 3:
				title_butn.setBackgroundResource(R.drawable.title_butn);
				intent = new Intent(HomeActivity.this,
						EnglishEditActivity.class);
				HomeActivity.this.setActivity(intent);

				break;

			case 4:
				title_butn.setBackgroundResource(R.drawable.title_butn);
				intent = new Intent(HomeActivity.this,
						EnglishEditActivity.class);
				HomeActivity.this.setActivity(intent);
				break;
			case 5:
				title_text.setText("平安校园");
				// MainActivity.resetschool();
				break;
			case 6:
				intent = new Intent(HomeActivity.this, MainActivity.class);
				HomeActivity.this.setActivity(intent);
				// MainActivity.ret();

				break;
			case 7:
				intent = new Intent(HomeActivity.this, SchoolMienActivity.class);
				HomeActivity.this.setActivity(intent);

				break;
			case 8:
				title_butn.setBackgroundResource(R.drawable.title_butn);
				intent = new Intent(HomeActivity.this,
						SuperCompositionActivity.class);
				HomeActivity.this.setActivity(intent);

				break;
			case 9:
				title_butn.setBackgroundResource(R.drawable.title_butn);
				intent = new Intent(HomeActivity.this, SynParseActivity.class);
				HomeActivity.this.setActivity(intent);

				break;
			case 10:
				title_butn.setBackgroundResource(R.drawable.title_butn);
				intent = new Intent(HomeActivity.this,
						ErrorLibraryActivity.class);
				HomeActivity.this.setActivity(intent);

				break;

			case 0:
				// Intent intent = new Intent(HomeActivity.this,
				// SchoolMienActivity.class);
				// HomeActivity.this.startActivity(intent);
				finish();

				break;

			}
			rutn = 0;

		}
		return false;
	}
}
